home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / misc / Resistor.lha / resistor / src / resistor_functions.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-15  |  19.3 KB  |  350 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*   Includes                                                            */
  4. /*                                                                       */
  5. /*************************************************************************/
  6.  
  7. #include "resistor_Includes.h"
  8. #include "resistor.h"
  9.  
  10. /*************************************************************************/
  11. /*                                                                       */
  12. /*   Variables and Structures                                            */
  13. /*                                                                       */
  14. /*************************************************************************/
  15.  
  16. extern struct IntuitionBase *IntuitionBase;
  17. extern struct GfxBase       *GfxBase;
  18.  
  19. extern struct UtilityBase   *UtilityBase;
  20.  
  21. extern struct Library *GadToolsBase;
  22. extern struct Library *AslBase;
  23. extern struct Library *DataTypesBase;
  24.  
  25. /*************************************************************************/
  26. /*                                                                       */
  27. /*   Defines                                                             */
  28. /*                                                                       */
  29. /*************************************************************************/
  30.  
  31. #define RASTERX (((struct GfxBase *)GfxBase)->DefaultFont->tf_XSize)
  32. #define RASTERY (((struct GfxBase *)GfxBase)->DefaultFont->tf_YSize)
  33.  
  34. #define XSIZE(x)  ((x)*RASTERX)
  35. #define YSIZE(x)  ((x)*RASTERY)
  36.  
  37. #define XPOS(x)   (XSIZE(x)+customscreen->WBorLeft)
  38. #define YPOS(x)   (YSIZE(x)+customscreen->BarHeight+1)
  39.  
  40. /*************************************************************************/
  41. /*                                                                       */
  42. /*   SleepWindow() und WakenWindow()                                     */
  43. /*                                                                       */
  44. /*************************************************************************/
  45.  
  46. static struct Requester waitrequest;
  47.  
  48. void SleepWindow(struct Window *win)
  49. {
  50.         InitRequester(&waitrequest);
  51.         if (win->FirstRequest == NULL ) Request(&waitrequest,win);
  52.         SetWindowPointer(win,WA_BusyPointer,1L,TAG_DONE);
  53. }
  54.  
  55. void WakenWindow(struct Window *win)
  56. {
  57.         if (win->FirstRequest != NULL) EndRequest(&waitrequest,win);
  58.         SetWindowPointer(win,WA_Pointer,0L,TAG_DONE);
  59. }
  60.  
  61. /*************************************************************************/
  62. /*                                                                       */
  63. /*   GUIC_ErrorReport()                                                  */
  64. /*                                                                       */
  65. /*************************************************************************/
  66.  
  67. void GUIC_ErrorReport(struct Window *win,ULONG type)
  68. {
  69.         char error[256];
  70.         struct EasyStruct easystruct = { sizeof(struct EasyStruct),0,"Caution:",NULL,"OK" };
  71.         easystruct.es_TextFormat     = error;
  72.  
  73.         switch (type)
  74.                 {
  75.                 case ERROR_NO_WINDOW_OPENED:
  76.                         strcpy(error,"Could not open window (no memory?)");
  77.                         break;
  78.                 case ERROR_NO_PUBSCREEN_LOCKED:;
  79.                         strcpy(error,"Could not lock pubscreen");
  80.                         break;
  81.                 case ERROR_NO_GADGETS_CREATED:
  82.                         strcpy(error,"Could not create gadgets");
  83.                         break;
  84.                 case ERROR_NO_GADGETLIST_CREATED:
  85.                         strcpy(error,"Could not create gadgetlist");
  86.                         break;
  87.                 case ERROR_NO_VISUALINFO:
  88.                         strcpy(error,"Could not read visualinfo from screen");
  89.                         break;
  90.                 case ERROR_NO_PICTURE_LOADED:
  91.                         strcpy(error,"Could not read picture data");
  92.                         break;
  93.                 case ERROR_NO_WINDOW_MENU:
  94.                         strcpy(error,"Could not create menu");
  95.                         break;
  96.                 case ERROR_SCREEN_TOO_SMALL:
  97.                         strcpy(error,"This screen is too small for the window");
  98.                         break;
  99.                 case ERROR_LIST_NOT_INITIALIZED:
  100.                         strcpy(error,"The attached list is not initialized!");
  101.                         break;
  102.                 default:
  103.                         Fault(type,"Error",error,sizeof(error));
  104.                 }
  105.         if (win && !win->FirstRequest)
  106.                 {
  107.                 SleepWindow(win);
  108.                 EasyRequestArgs(win,&easystruct,NULL,NULL);
  109.                 WakenWindow(win);
  110.                 }
  111.         else EasyRequestArgs(win,&easystruct,NULL,NULL);
  112. }
  113.  
  114. /*************************************************************************/
  115. /*                                                                       */
  116. /*   CreateBevelFrames()                                                 */
  117. /*                                                                       */
  118. /*************************************************************************/
  119.  
  120. void CreateBevelFrames(struct Window *win,APTR visualinfo,ULONG bevelcount,struct BevelFrame bevels[])
  121. {
  122.         ULONG i;
  123.         for (i=0;i<bevelcount;i++)
  124.                 {
  125.                 DrawBevelBox(win->RPort,bevels[i].bb_LeftEdge,bevels[i].bb_TopEdge,bevels[i].bb_Width,bevels[i].bb_Height,GT_VisualInfo,(ULONG)visualinfo,GTBB_Recessed,TRUE,TAG_END);
  126.                 DrawBevelBox(win->RPort,bevels[i].bb_LeftEdge+2,bevels[i].bb_TopEdge+1,bevels[i].bb_Width-4,bevels[i].bb_Height-2,GT_VisualInfo,(ULONG)visualinfo,TAG_END);
  127.                 if (bevels[i].bb_Title)
  128.                         {
  129.                         char title[64];
  130.                         sprintf(title," %s ",bevels[i].bb_Title);
  131.                         Move(win->RPort,bevels[i].bb_LeftEdge+(bevels[i].bb_Width-XSIZE(strlen(title)))/2,bevels[i].bb_TopEdge+2);
  132.                         SetAPen(win->RPort,bevels[i].bb_Color);
  133.                         Text(win->RPort,title,strlen(title));
  134.                         }
  135.                 }
  136. }
  137.  
  138. /*************************************************************************/
  139. /*                                                                       */
  140. /*   CreateLines()                                                       */
  141. /*                                                                       */
  142. /*************************************************************************/
  143.  
  144. void CreateLines(struct Window *win,int linecount,struct Line lines[])
  145. {
  146.         ULONG i;
  147.         for (i=0;i<linecount;i++)
  148.                 {
  149.                 SetAPen(win->RPort,lines[i].li_Color);
  150.                 Move(win->RPort,lines[i].li_LeftEdge,lines[i].li_TopEdge);
  151.                 Draw(win->RPort,lines[i].li_Width>0?lines[i].li_LeftEdge+lines[i].li_Width-1:lines[i].li_LeftEdge+lines[i].li_Width,lines[i].li_Height>0?lines[i].li_TopEdge+lines[i].li_Height-1:lines[i].li_TopEdge+lines[i].li_Height);
  152.                 }
  153. }
  154.  
  155. /*************************************************************************/
  156. /*                                                                       */
  157. /*   CreateTexts()                                                       */
  158. /*                                                                       */
  159. /*************************************************************************/
  160.  
  161. void CreateTexts(struct Window *win,int textcount,struct Text texts[], long double xscale,long double yscale)
  162. {
  163.         ULONG i;
  164.         for (i=0;i<textcount;i++)
  165.                 {
  166.                 SetAPen(win->RPort,texts[i].tx_Color);
  167.                 Move(win->RPort,texts[i].tx_LeftEdge,texts[i].tx_TopEdge+(ULONG)(yscale*((struct GfxBase *)GfxBase)->DefaultFont->tf_Baseline));
  168.                 Text(win->RPort,texts[i].tx_Text,strlen(texts[i].tx_Text));
  169.                 }
  170. }
  171.  
  172. /*************************************************************************/
  173. /*                                                                       */
  174. /*   ShowGadget()                                                        */
  175. /*                                                                       */
  176. /*************************************************************************/
  177.  
  178. #define GADGET_DOWN  0
  179. #define GADGET_UP    1
  180.  
  181. void ShowGadget(struct Window *win, struct Gadget *gad, int type)
  182. {
  183.         if ((gad->Flags & GFLG_DISABLED) == 0)
  184.                 {
  185.                 int gadpos = RemoveGadget(win, gad);
  186.  
  187.                 if (type == GADGET_DOWN)
  188.                         gad->Flags |= GFLG_SELECTED;
  189.                 else
  190.                         gad->Flags &= ~GFLG_SELECTED;
  191.  
  192.                 AddGadget(win, gad, gadpos);
  193.                 RefreshGList(gad, win, NULL, 1);
  194.         }
  195. }
  196.  
  197. /*************************************************************************/
  198. /*                                                                       */
  199. /*   About()                                                             */
  200. /*                                                                       */
  201. /*************************************************************************/
  202.  
  203. void About(struct Window *hostwin,struct Gadget **wingads,APTR userdata)
  204. {
  205.         APTR visualinfo;
  206.         Object *o;
  207.         struct Gadget   *gadgetlist   = NULL;
  208.         struct Screen   *customscreen = NULL;
  209.         struct Gadget   *gadget       = NULL;
  210.         struct TextAttr  textattr     = { NULL,0,FS_NORMAL,FPF_DISKFONT };
  211.         struct NewGadget newgad;
  212.         struct Gadget   *textfield    = NULL;
  213.         visualinfo=GetVisualInfo(hostwin->WScreen,TAG_DONE);
  214.         if (visualinfo)
  215.                 {
  216.                 o=NewDTObject("PROGDIR:About.iff",DTA_SourceType,DTST_FILE,DTA_GroupID,GID_PICTURE,TAG_DONE);
  217.                 if (o)
  218.                         {
  219.                         customscreen = hostwin->WScreen;
  220.                         gadget       = CreateContext(&gadgetlist);
  221.                         if (gadget)
  222.                                 {
  223.                                 ULONG height=24,width=35,left=0,top=0;
  224.                                 struct Gadget *wingad;
  225.                                 char * title = "About";
  226.                                 struct Window *win = NULL;
  227.  
  228.                                 textattr.ta_Name     = ((struct GfxBase *)GfxBase)->DefaultFont->tf_Message.mn_Node.ln_Name;
  229.                                 textattr.ta_YSize    = ((struct GfxBase *)GfxBase)->DefaultFont->tf_YSize;
  230.                                 newgad.ng_LeftEdge   = XPOS(1);
  231.                                 newgad.ng_TopEdge    = YPOS(21);
  232.                                 newgad.ng_Width      = XSIZE(33)+200;
  233.                                 newgad.ng_Height     = YSIZE(2);
  234.                                 newgad.ng_GadgetText = "_OK";
  235.                                 newgad.ng_TextAttr   = &textattr;
  236.                                 newgad.ng_GadgetID   = 2;
  237.                                 newgad.ng_Flags      = PLACETEXT_IN;
  238.                                 newgad.ng_VisualInfo = visualinfo;
  239.                                 newgad.ng_UserData   = NULL;
  240.  
  241.                                 height= YSIZE(height);
  242.                                 width = XSIZE(width)+200;
  243.                                 left  = (customscreen->Width-width)/2;
  244.                                 top   = (customscreen->Height-height)/2;
  245.  
  246.                                 wingad = gadget = CreateGadget(BUTTON_KIND,gadget,&newgad,GT_Underscore,'_',TAG_END);
  247.  
  248.                                 if (height>customscreen->Height || width>customscreen->Width) GUIC_ErrorReport(hostwin,ERROR_SCREEN_TOO_SMALL);
  249.                                 win=OpenWindowTags(NULL,WA_Activate,         TRUE,
  250.                                         WA_CloseGadget,      TRUE,
  251.                                         WA_DepthGadget,      TRUE,
  252.                                         WA_SizeGadget,       FALSE,
  253.                                         WA_DragBar,          TRUE,
  254.                                         WA_Gadgets,          (ULONG)gadgetlist,
  255.                                         WA_InnerHeight,      height,
  256.                                         WA_InnerWidth,       width,
  257.                                         WA_IDCMP,            IDCMP_CLOSEWINDOW|BUTTONIDCMP|IDCMPUPDATE|IDCMP_VANILLAKEY,
  258.                                         WA_Left,             left,
  259.                                         WA_Top,              top,
  260.                                         WA_SmartRefresh,     TRUE,
  261.                                         WA_Title,            (ULONG)title,
  262.                                         WA_CustomScreen,     (ULONG)customscreen,
  263.                                         TAG_END);
  264.                                 if (win)
  265.                                         {
  266.                                         struct IntuiMessage  *imessage   = NULL;
  267.                                         struct Gadget        *idcmpgad   = NULL;
  268.                                         struct BitMap        *bmcopy     = NULL;
  269.                                         struct TagItem       *tag        = NULL;
  270.                                         struct TagItem       *tstate     = NULL;
  271.                                         ULONG  idcmpclass                = 0;
  272.                                         UWORD  messagecode               = 0;
  273.                                         BOOL   running                   = TRUE;
  274.  
  275.                                         textfield = (struct Gadget *)NewObject(TEXTFIELD_GetClass(), NULL,
  276.                                                 GA_ID,               4711,
  277.                                                 GA_Top,              YPOS(1),
  278.                                                 GA_Left,             XPOS(3)+200,
  279.                                                 GA_Width,            XSIZE(31),
  280.                                                 GA_Height,           YSIZE(19),
  281.                                                 TEXTFIELD_TextAttr,  (ULONG)&textattr,
  282.                                                 TEXTFIELD_Text,      (ULONG)"\nWiderstand\n(Resistor)\n1.0\nby\nAlfred Faust\nwritten September 2000\nalfred.j.faust@gmx.de\n\nFor Calculation and/or\nDecoding of the colored rings at Resistors.\n\nFREEWARE\n\n! Use it at your own risk !",
  283.                                                 TEXTFIELD_Border,    TEXTFIELD_BORDER_BEVEL,
  284.                                                 TEXTFIELD_Alignment, TEXTFIELD_ALIGN_CENTER,
  285.                                                 TEXTFIELD_Inverted,  TRUE,
  286.                                                 TEXTFIELD_ReadOnly,  TRUE,
  287.                                                 TAG_END);
  288.  
  289.                                         if (textfield)
  290.                                                 {
  291.                                                 AddGadget(win,textfield,-1);
  292.                                                 RefreshGList(textfield,win,NULL,-1);
  293.  
  294.                                                 SleepWindow(hostwin);
  295.                                                 SetFont(win->RPort,((struct GfxBase *)GfxBase)->DefaultFont);
  296.                                                 DrawBevelBox(win->RPort,XPOS(1),YPOS(1),204,102,GT_VisualInfo,(ULONG)visualinfo,GTBB_Recessed,TRUE,TAG_END);
  297.                                                 SetDTAttrs(o,NULL,NULL,GA_Left,XPOS(1)+2,GA_Top,YPOS(1)+1,GA_Width,200,GA_Height,100,PDTA_Remap,TRUE,PDTA_DestBitMap,(ULONG)&bmcopy,ICA_TARGET,ICTARGET_IDCMP,TAG_DONE);
  298.                                                 AddDTObject(win,NULL,o,-1L);
  299.                                                 GT_RefreshWindow(win,NULL);
  300.                                                 while (running)
  301.                                                         {
  302.                                                         Wait(1L << win->UserPort->mp_SigBit);
  303.                                                         while (imessage=GT_GetIMsg(win->UserPort))
  304.                                                                 {
  305.                                                                 idcmpgad=(struct Gadget *)imessage->IAddress;
  306.                                                                 idcmpclass=imessage->Class;
  307.                                                                 messagecode =imessage->Code;
  308.                                                                 GT_ReplyIMsg(imessage);
  309.                                                                 switch(idcmpclass)
  310.                                                                         {
  311.                                                                         case IDCMP_VANILLAKEY:
  312.                                                                                 if (messagecode == 27 || messagecode == 'o' || messagecode == 'O') running=FALSE;
  313.                                                                                 break;
  314.                                                                         case IDCMP_REFRESHWINDOW:
  315.                                                                                 GT_BeginRefresh(win);
  316.                                                                                 GT_EndRefresh(win,TRUE);
  317.                                                                                 break;
  318.                                                                         case IDCMP_CLOSEWINDOW:
  319.                                                                                 running=FALSE;
  320.                                                                                 break;
  321.                                                                         case BUTTONIDCMP:
  322.                                                                                 running=FALSE;
  323.                                                                                 break;
  324.                                                                         case IDCMP_IDCMPUPDATE:
  325.                                                                                 tstate=(struct TagItem*)imessage->IAddress;
  326.                                                                                 while (tag=NextTagItem(&tstate)) if (tag->ti_Tag == DTA_Sync) RefreshDTObjectA(o,win,NULL,NULL);
  327.                                                                                 break;
  328.                                                                         }
  329.                                                                 }
  330.                                                         }
  331.                                                 RemoveGadget(win,textfield);
  332.                                                 DisposeObject((APTR)textfield);
  333.                                                 }
  334.                                         else GUIC_ErrorReport(hostwin,ERROR_NO_GADGETLIST_CREATED);
  335.                                         CloseWindow(win);
  336.                                         WakenWindow(hostwin);
  337.                                         }
  338.                                 else GUIC_ErrorReport(hostwin,ERROR_NO_WINDOW_OPENED);
  339.                                 FreeGadgets(gadgetlist);
  340.                                 }
  341.                         else GUIC_ErrorReport(hostwin,ERROR_NO_GADGETLIST_CREATED);
  342.                         DisposeDTObject(o);
  343.                         }
  344.                 else GUIC_ErrorReport(hostwin,ERROR_NO_PICTURE_LOADED);
  345.                 FreeVisualInfo(visualinfo);
  346.                 }
  347.         else GUIC_ErrorReport(hostwin,ERROR_NO_VISUALINFO);
  348.  
  349. }
  350.